//
//  WXXTabClickPageController.m
//  WanxxPageController
//
//  Created by myApple on 16/5/27.
//  Copyright © 2016年 myApple. All rights reserved.
//

#import "WXXTabClickPageController.h"
#import "WXXTitleViewCell.h"

@interface WXXTabClickPageController ()
@property (nonatomic, assign) CGFloat selectFontScale;
@end

@implementation WXXTabClickPageController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        
        _normalTextColor = [UIColor blackColor];
        _selectedTextColor = UIColorFromRGB(0x7dc234);
        
        _pagerBarColor = [UIColor whiteColor];
        _collectionViewBarColor = [UIColor whiteColor];
        
        _progressColor = UIColorFromRGB(0x7dc234);
        _progressRadius = self.progressHeight/2;
        
        [self registerCellClass:[WXXTitleViewCell class] isContainXib:NO];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;
    if (!self.dataSource) {
        self.dataSource = self;
    }
    _selectFontScale = 1.0;
    
    [self configureSubViews];
    // Do any additional setup after loading the view.
}

- (void)configureSubViews
{
    // progress
    self.progressView.backgroundColor = _progressColor;
    self.progressView.layer.cornerRadius = _progressRadius;
    self.progressView.layer.masksToBounds = YES;
    
    // tabBar
    self.pagerBarView.backgroundColor = _pagerBarColor;
    self.collectionViewBar.backgroundColor = _collectionViewBarColor;
}

- (void)setBarStyle:(TYPagerBarStyle)barStyle
{
    [super setBarStyle:barStyle];
    
    if (barStyle == TYPagerBarStyleCoverView) {
        self.progressColor = [UIColor lightGrayColor];
    }else {
        self.progressColor = UIColorFromRGB(0x7dc234);
    }
    self.progressRadius = self.progressHeight/2;
}

#pragma mark - private

- (void)transitionFromCell:(UICollectionViewCell<WXXTitleViewCellProtocol> *)fromCell toCell:(UICollectionViewCell<WXXTitleViewCellProtocol> *)toCell
{
    if (fromCell) {
        fromCell.titleLabel.textColor = self.normalTextColor;
        fromCell.transform = CGAffineTransformMakeScale(self.selectFontScale, self.selectFontScale);
    }
    
    if (toCell) {
        toCell.titleLabel.textColor = self.selectedTextColor;
        toCell.transform = CGAffineTransformIdentity;
    }
}

- (void)transitionFromCell:(UICollectionViewCell<WXXTitleViewCellProtocol> *)fromCell toCell:(UICollectionViewCell<WXXTitleViewCellProtocol> *)toCell progress:(CGFloat)progress
{
    fromCell.titleLabel.textColor = [UIColor blackColor];
    toCell.titleLabel.textColor = [UIColor greenColor];
}

#pragma mark - TYPagerControllerDataSource

- (NSInteger)numberOfControllersInPagerController
{
    NSAssert(NO, @"you must impletement method numberOfControllersInPagerController");
    return 0;
}

- (UIViewController *)pagerController:(WXXPageController *)pagerController controllerForIndex:(NSInteger)index
{
    NSAssert(NO, @"you must impletement method pagerController:controllerForIndex:");
    return nil;
}

#pragma mark - TYTabPagerControllerDelegate

- (void)pagerController:(WXXTabPageController *)pagerController configreCell:(WXXTitleViewCell *)cell forItemTitle:(NSString *)title atIndexPath:(NSIndexPath *)indexPath
{
    WXXTitleViewCell *titleCell = (WXXTitleViewCell *)cell;
    titleCell.titleLabel.text = title;
    titleCell.titleLabel.font = self.selectedTextFont;
}

- (void)pagerController:(WXXTabPageController *)pagerController transitionFromeCell:(UICollectionViewCell<WXXTitleViewCellProtocol> *)fromCell toCell:(UICollectionViewCell<WXXTitleViewCellProtocol> *)toCell animated:(BOOL)animated
{
    if (animated) {
        [UIView animateWithDuration:self.animateDuration animations:^{
            [self transitionFromCell:(WXXTitleViewCell *)fromCell toCell:(WXXTitleViewCell *)toCell];
        }];
    }else{
        [self transitionFromCell:fromCell toCell:toCell];
    }
}

- (void)pagerController:(WXXTabPageController *)pagerController transitionFromeCell:(UICollectionViewCell<WXXTitleViewCellProtocol> *)fromCell toCell:(UICollectionViewCell<WXXTitleViewCellProtocol> *)toCell progress:(CGFloat)progress
{
    [self transitionFromCell:fromCell toCell:toCell progress:progress];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
